#include "titlebar.h"
#include "ui_titlebar.h"

TitleBar::TitleBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TitleBar)
{
    ui->setupUi(this);

    loadStyleSheet("C:/Users/shafl/Desktop/FrameWork/css/img.css");

    //QPalette widgetPalette;
    //widgetPalette.setColor(QPalette::Window, QColor(255, 170, 127));
    //this->setPalette(widgetPalette);

    this->setBackgroundRole(QPalette::Dark);
    this->setAutoFillBackground(true);      // 必须调用，否则不生效

    connect(ui->btn_close,&QPushButton::clicked,this,&TitleBar::onClose);
    connect(ui->btn_max,&QPushButton::clicked,this,&TitleBar::onMaxSized);
    connect(ui->btn_min,&QPushButton::clicked,this,&TitleBar::onMinSized);
}

TitleBar::~TitleBar()
{
    delete ui;
}

void TitleBar::loadStyleSheet(const QString &CssPath)
{
    QFile file(CssPath);
    if(!file.open(QIODevice::ReadWrite)){
        qDebug() << "open file failed";
        return;
    }
    this->setStyleSheet(file.readAll());
}

void TitleBar::onClose()
{
    if(!this->parentWidget()->isMaximized()){
        m_settingManage.setValue("WindowPosition/x",this->parentWidget()->frameGeometry().left());
        m_settingManage.setValue("WindowPosition/y",this->parentWidget()->frameGeometry().top());
        m_settingManage.setValue("WindowPosition/width",this->parentWidget()->geometry().width());
        m_settingManage.setValue("WindowPosition/height",this->parentWidget()->geometry().height());
        m_settingManage.setValue("WindowPosition/MaxWidget",this->parentWidget()->isMaximized());
        m_settingManage.sync();
    }
    else{
        m_settingManage.setValue("WindowPosition/MaxWidget",this->parentWidget()->isMaximized());
        m_settingManage.sync();
    }
    this->parentWidget()->close();
}

void TitleBar::onMinSized()
{
    this->parentWidget()->showMinimized();
}

void TitleBar::onMaxSized()
{
    if(!this->parentWidget()->isMaximized()){
        this->parentWidget()->showMaximized();
    }
    else{
        this->parentWidget()->showNormal();
    }
}
